package com.weixinli.web.actions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.imageio.stream.FileImageInputStream;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.weixinli.beans.FmArticle;
import com.weixinli.beans.PageInfo;
import com.weixinli.biz.IFmArticleBiz;
import com.weixinli.utils.PageUtil;
import com.weixinli.web.actions.model.JsonObject;
@Controller("BackFmArticleAction")
public class BackFmArticleAction extends ActionSupport implements ModelDriven<FmArticle> {
	private static final long serialVersionUID = 1L;
	
	private JsonObject jsonObject;
	
	private FmArticle fmArticle;
	
	private int page;  //显示的页数
	private int rows;  //显示的条数
	public List<File> file;   
	public List<String> fileContentType;
	public List<String> fileFileName;
	public String savePath;
	
	private IFmArticleBiz ifmArticleBiz;
	
	private String fids;
	/**
	 * 上传图片处理的action
	 * @return
	 */
	public String addFmActicle(){
		
		FileOutputStream fos=null;
		FileImageInputStream fis=null;
		String addr=null;   //存放图片
		String Aduio=null;  //存放音频
		boolean success=true;
		//对于文件图上的处理
		for(int i=0;i<file.size();i++){
			try {
				addr=new Date().getTime()+""+new Random().nextInt(1000)+fileFileName.get(i);
				fos=new FileOutputStream(getSavePath()+"\\"+addr);
				fis=new FileImageInputStream(file.get(i));
				
				byte[] buffer=new byte[2048];
				int len=0;
				while((len=fis.read(buffer))>0){
					fos.write(buffer,0,len);
				}
				fos.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				success=false;
			} catch (IOException e) {
				e.printStackTrace();
				success=false;
			}finally{
				if(fos!=null){
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
						success=false;
					}
				}
				if(fis!=null){
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
						success=false;
					}
				}
			}
		}
		
		fmArticle.setFaddr(savePath+"/"+addr);
		int data=ifmArticleBiz.addFmArticle(fmArticle);
		FmArticle fid=ifmArticleBiz.selectFid(fmArticle);
		if(fid.getFid()>0){
			jsonObject.setObj(fid.getFid());
		}else{
			jsonObject.setObj(0);
		}
		return "success";
	}
	/**
	 * 对于音频的上传
	 * @return
	 */
	public String addFmActicleAudio(){
		FileOutputStream fos=null;
		FileImageInputStream fis=null;
		String name=null;
		boolean success=true;
		//对于文件图上的处理
		for(int i=0;i<file.size();i++){
			try {
				name=new Date().getTime()+""+new Random().nextInt(1000)+fileFileName.get(i);
				fos=new FileOutputStream(getSavePath()+"\\"+name);
				fis=new FileImageInputStream(file.get(i));	
				byte[] buffer=new byte[2048];
				int len=0;
				while((len=fis.read(buffer))>0){
					fos.write(buffer,0,len);
				}
				fos.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				success=false;
			} catch (IOException e) {
				e.printStackTrace();
				success=false;
			}finally{
				if(fos!=null){
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
						success=false;
					}
				}
				if(fis!=null){
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
						success=false;
					}
				}
			}
		}
		if(success){
			fmArticle.setfAudio(savePath+"/"+name);  //这里吗？？？
			SimpleDateFormat ft=new SimpleDateFormat("yyyy/MM/dd  HH:mm:ss");
			fmArticle.setFtime(ft.format(new Date()));
			int data=ifmArticleBiz.insertAduio(fmArticle);
			if(data>0){
				jsonObject.setObj(data);
				return "success";
			}else{
				
				jsonObject.setObj(0);
				return "success";
			}
		}else{
			jsonObject.setObj(0);
			return "success";
		}
	}
	
	/**
	 * 后台查询信息
	 * @return
	 */
	public String SelectFmActicle(){
		List<FmArticle> fmAll=ifmArticleBiz.findAll(new PageInfo(page,rows));
		if(fmAll.size()>0){
			jsonObject.setRows(fmAll);
			jsonObject.setTotal(fmAll.size());
			return "success";
		}
		return "success";
	}
	/**
	 * 后台删除的
	 * @return
	 */
	public String DelFmArticle(){
		FmArticle fm=new FmArticle();
		String[] fid=fids.split(",");
		int data=0;
		for(int i=0;i<fid.length;i++){
			int f=Integer.valueOf(fid[i]);
			fm.setFid(f);
			data=ifmArticleBiz.delFmArticle(fm);
		}
		if(data>0){
			jsonObject.setResult(data);
		}else{
			jsonObject.setResult(0);
		}
		return "success";
	}
	
	public FmArticle getModel() {
		fmArticle=new FmArticle();
		return fmArticle;
	}
	public FmArticle getFmArticle() {
		return fmArticle;
	}
	public void setFmArticle(FmArticle fmArticle) {
		this.fmArticle = fmArticle;
	}
	@Resource(name="ifmArticleBiz")
	public void setiFmArticleBiz(IFmArticleBiz iFmArticleBiz) {
		this.ifmArticleBiz = iFmArticleBiz;
	}
	@Resource(name="jsonObject")
	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	public JsonObject getJsonObject() {
		return jsonObject;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public void setFids(String fids) {
		this.fids = fids;
	}
}
